package com.gm.baseplatform.base.module;

import com.gm.baseplatform.core.tree.TreeNode;
import com.gm.baseplatform.core.tree.Treeable;
import com.gm.baseplatform.core.utils.ApplicationContextUtil;
import com.gm.baseplatform.core.utils.StringUtil;
/**
 * 
 * 基础模块配置类,支持多级子模块配置
 * 
 * @author wangguomei
 * @mail e.wanggm@gmail.com
 * 
 */
public class Module implements Treeable {
	
	public final static String NON_PARENT = "-1";
	public final static String NON_PARENT_NAME = "一级模块";

	/**唯一标识*/
	private String unid;
	
	/**模块名称-中文*/
	private String name;
	
	/**美化图片路径*/
	private String imgPath;
	
	/**事件动作*/
	private String performAction;
	
	/**同级模块位置排序*/
	private Integer position;
	
	/**所属父级模块,一级模块默认值为-1*/
	private String parent;
	
	public String getUnid() {
		return unid;
	}

	public void setUnid(String unid) {
		this.unid = unid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getImgPath() {
		return imgPath;
	}

	public void setImgPath(String imgPath) {
		this.imgPath = imgPath;
	}

	public String getPerformAction() {
		return performAction;
	}

	public void setPerformAction(String performAction) {
		this.performAction = performAction;
	}

	public Integer getPosition() {
		return position;
	}

	public void setPosition(Integer position) {
		this.position = position;
	}

	public String getParent() {
		return parent;
	}

	public void setParent(String parent) {
		this.parent = parent;
	}
	
	public String toString() {
		StringBuffer beanInfo = new StringBuffer();
		beanInfo.append("Module[");
		beanInfo.append("unid:" + unid);
		beanInfo.append(",name:" + name);
		beanInfo.append(",imgPath:" + imgPath);
		beanInfo.append(",performAction:" + performAction);
		beanInfo.append(",position:" + position);
		beanInfo.append(",parent:" + parent);
		beanInfo.append("]");
		return beanInfo.toString();
	}

	public TreeNode buildTreeNode() {
		TreeNode treeNode = new TreeNode();
		treeNode.setId(unid);
		treeNode.setText(name);
		treeNode.setChecked(false);
		ModuleManager mm = (ModuleManager)ApplicationContextUtil.getBean("moduleManager");
		if (mm.getChildCount(unid) <= 0) {
			treeNode.setHasChildren(false);
		} else {
			treeNode.setHasChildren(true);
		}
		return treeNode;
	}

	public String getTreeRootName() {
		return "模块树";
	}
	
	public boolean isRight() {
		if (StringUtil.isEmpty(name)) {
			return false;
		}
		if (StringUtil.isEmpty(imgPath)) {
			return false;
		}
		if (StringUtil.isEmpty(performAction)) {
			return false;
		}
		if (StringUtil.isEmpty(parent)) {
			return false;
		}
		return true;
	}
}
